// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var router = require("../hash/pageRouter");
	var css_tallyDetail = require("./fillTallyDetailInfo.css");
	var dialog = require("dialog");
	var validate = require("validate");
	var tools = require("tools");
	var databind = require("databind");
	var pageIndex = 1,
		pageSize = 10,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
	var imgSrc;
	var containerId;
	var type;
	var pickupId;
	var driverAddress = "";
	var left;
	var init = function(page) {
		containerId = page.params.containerId;
		pickupId = page.params.pickupId;
		type = page.params.type;
		showOnselfTally();
		databind.fillInput1($("#fillTallyDetailInfo"), {action: "inquireExchangeContainerInventoryDetail.json?containerId=" + containerId}, function(data) {
			databind.fillForm($("#fillTallyDetailInfo"), data.datas[0].exchangeContainerBulkstock[0]);
			$.each((data.datas[0].tallyPreFile).split(','), function() {
				var html = '<label class="finance-file"><a target="_blank" href="/finance/api/1.0/downloadFile.file?fileUuid=' + this + '"><img  src="/finance/api/1.0/downloadFile.file?fileUuid=' + this + '" class="finance-field-img"  alt="' + this + '" /></label>';
				$("label[for=tallyPreFile]").before(html);
			});
			
			$.each((data.datas[0].tallySuffixFile).split(','), function() {
				var html = '<label class="finance-file"><a target="_blank" href="/finance/api/1.0/downloadFile.file?fileUuid=' + this + '"><img  src="/finance/api/1.0/downloadFile.file?fileUuid=' + this + '" class="finance-field-img"  alt="' + this + '" /></label>';
				$("label[for=tallySuffixFile]").before(html);
			});
			
			$.each((data.datas[0].tallyExAbnormalFile).split(','), function() {
				var html = '<label class="finance-file"><a target="_blank" href="/finance/api/1.0/downloadFile.file?fileUuid=' + this + '"><img  src="/finance/api/1.0/downloadFile.file?fileUuid=' + this + '" class="finance-field-img"  alt="' + this + '" /></a></label>';
				$("label[for=tallyExAbnormalFile]").before(html);
			});
			
			if (type == "readOnly") {
				databind.disableInput($("#fillTallyDetailInfo"));
				$("#submitTally").remove();
				$("label[for=tallyExAbnormalFile]").remove();
				$("label[for=tallySuffixFile]").remove();
				$("label[for=tallyPreFile]").remove();
				$(".operateInfo").removeClass("none");
			}
		});
		initEvent();
		router.init();

		$("#leftMenu1").find(".leftMenu-item").removeClass("leftMenu-active");
		$("#leftMenu1 #newTaskList").parent(".leftMenu-item").addClass("leftMenu-active");
	};
	var initEvent = function() {
		//若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			top.location.href = "../login/login.html";
		}
		
		//提交
		$("#submitTally").unbind("click").click(function(){
			databind.submitDom($("#submitContainerTallying"), function(){
				base.alert(1, "提交成功！");
				router.route("main/pickupDetail.html?pickupId=" + pickupId);
			});
		});

		//查看理货任务
		$(".page-header").on("click", ".undoneCargoRecord", function() {
			router.route("newTaskList/undoneCargoRecord.html?recieveNoticeId=" + recieveNoticeId, true);
		});

		$(".page-header").on("click", ".taskDetail", function() {
			router.route("newTaskList/newTaskDetail.html?recieveNoticeId=" + recieveNoticeId + "&noticeState=" + noticeState + "&left=" + left, true);
		});

		//点击
		$(".newTaskList").unbind("click").click(function() {
			top.location.href = "../newTaskList/newTaskList.html?noticeState=" + noticeState + "&left=" + left;
		});
		
		$("#tallyDetail").on("change", ".caseNumber", function(){
			var $this = $(this);
			tools.matchs(/^[a-zA-Z]{4}[0-9]{7}$/, $this.val(), "请输入正确的箱号！");
		});

		//上传异常照片
		$("#tallyDetail").on("change", "#uploadFileAbnormalFile1", function() {

			uploadFileImg($(this).parent().parent().attr("id"), $(this).attr("id"));
		});

		//上传异常录音
		$("#tallyDetail").on("change", "#uploadFileSoundRecorde1", function() {

			uploadFile($(this).parent().parent(), "uploadFileSoundRecorde1");
		});

		//上传理货前照片
		$("#fillTallyDetailInfo").on("change", "#tallyPreFile", function() {
			tools.uploadFile("tallyPreFile", function(data){
				imgSrc = data.data[0].fileUuid;
				var html = "<label class=\"finance-file\"><img  src=\"/finance/api/1.0/downloadFile.file?fileUuid=" + imgSrc + "\" class=\"finance-field-img\"  alt=\"" + imgSrc + "\"  /> <a class=\"closeLayer\" ></a></label>";
				$("label[for=tallyPreFile]").before(html);
				var fs = [];
				$.each($("#uploadFilepreTallyP img"), function() {
					fs.push($(this).attr("alt"));
				});
				$("#uploadFilepreTallyP input[name=tallyPreFile]").val(fs);
			});
		});
		
		//上传理货后照片
		$("#fillTallyDetailInfo").on("change", "#tallySuffixFile", function() {
			tools.uploadFile("tallySuffixFile", function(data){
				imgSrc = data.data[0].fileUuid;
				var html = "<label class=\"finance-file\"><img  src=\"/finance/api/1.0/downloadFile.file?fileUuid=" + imgSrc + "\" class=\"finance-field-img\"  alt=\"" + imgSrc + "\"  /> <a class=\"closeLayer\" ></a></label>";
				$("label[for=tallySuffixFile]").before(html);
				var fs = [];
				$.each($("#uploadFilesuffixTallyP img"), function() {
					fs.push($(this).attr("alt"));
				});
				$("#uploadFilesuffixTallyP input[name=tallySuffixFile]").val(fs);
			});
		});
		
		//上传异常图片
		$("#fillTallyDetailInfo").on("change", "#tallyExAbnormalFile", function() {
			tools.uploadFile("tallyExAbnormalFile", function(data){
				imgSrc = data.data[0].fileUuid;
				var html = "<label class=\"finance-file\"><img  src=\"/finance/api/1.0/downloadFile.file?fileUuid=" + imgSrc + "\" class=\"finance-field-img\"  alt=\"" + imgSrc + "\"  /> <a class=\"closeLayer\" ></a></label>";
				$("label[for=tallyExAbnormalFile]").before(html);
				var fs = [];
				$.each($("#abnormalFileP img"), function() {
					fs.push($(this).attr("alt"));
				});
				$("#abnormalFileP input[name=tallyExAbnormalFile]").val(fs);
			});
		});

		//删除理货前照片
		$("#uploadFilepreTallyP").on("click", ".closeLayer", function() {
			$(this).parent().parent().find(".file").css("display", "inline-block");
			$(this).parent().remove();
			var fs = [];
			$.each($("#uploadFilepreTallyP img"), function() {
				fs.push($(this).attr("alt"));
			});
			$("#uploadFilepreTallyP input[name=tallyPreFile]").val(fs);
		});
		
		//删除理货后照片
		$("#uploadFilesuffixTallyP").on("click", ".closeLayer", function() {
			$(this).parent().parent().find(".file").css("display", "inline-block");
			$(this).parent().remove();
			var fs = [];
			$.each($("#uploadFilesuffixTallyP img"), function() {
				fs.push($(this).attr("alt"));
			});
			$("#uploadFilesuffixTallyP input[name=tallySuffixFile]").val(fs);
		});
		
		//删除异常照片
		$("#abnormalFileP").on("click", ".closeLayer", function() {
			$(this).parent().parent().find(".file").css("display", "inline-block");
			$(this).parent().remove();
			var fs = [];
			$.each($("#abnormalFileP img"), function() {
				fs.push($(this).attr("alt"));
			});
			$("#abnormalFileP input[name=tallyExAbnormalFile]").val(fs);
		});
		
		//点击理货异常
		$(".tallyAbnormal").on("change", function() {
			if ($(this).val() == 0) {
				$(".abnormalDiv").removeClass("none");
			} else {
				$(".abnormalDiv").addClass("none");
			}
		});

	};
	//初始化比例
	var initScale = function() {
			var bulkstock;
			$.each($(".tallyQuantity"), function(i, val) {
				var bulkstockId = $(this).attr("bulkstockId");
				$.each(taskDetail.datas[0].bulkstocks, function(i, val) {
					if (bulkstockId == val.bulkstockId) {
						bulkstock = val;
					}
				});
				var tallyQuantity = $(this).val();
				var max = $(this).attr("max");
				var maxWeight = $(this).attr("maxWeight");
				var maxVolume = $(this).attr("maxVolume");
				var tallyWeight = $(this).parent().parent().find(".tallyWeight").val();
				var tallyVolume = $(this).parent().parent().find(".tallyVolume").val();
				$(this).parent().parent().find(".differenceValueQuantity").html(tallyQuantity - max);
				$(this).parent().parent().find(".differenceValueWeight").html(tallyWeight - maxWeight);
				$(this).parent().parent().find(".differenceValueVolume").html(tallyVolume - maxVolume);
				//				$(this).attr("weightScale", bulkstock.weight / bulkstock.quantity); //重量比例
				//				$(this).attr("volumeScale", bulkstock.volume / bulkstock.quantity); //体积比例
			});

			//绑定理货数量值改变事件
			$(".tallyQuantity").unbind("change").change(function() {
				var tallyQuantity = $(this).val();
				var max = $(this).attr("max");
				var weightScale = $(this).attr("weightScale");
				var volumeScale = $(this).attr("volumeScale");
				//			if(parseFloat(tallyQuantity)>parseFloat(max)){
				//					base.alert(2, "理货数量不能大于计划理货数量"+max);
				//			}
				var maxWeight = $(this).attr("maxWeight");
				var maxVolume = $(this).attr("maxVolume");

				//				$(this).parent().parent().find(".tallyWeight").val((tallyQuantity * weightScale).toFixed(0));
				//				$(this).parent().parent().find(".tallyVolume").val((tallyQuantity * volumeScale).toFixed(0));
				var tallyWeight = $(this).parent().parent().find(".tallyWeight").val();
				var tallyVolume = $(this).parent().parent().find(".tallyVolume").val();
				$(this).parent().parent().find(".differenceValueQuantity").html(tallyQuantity - max);
				$(this).parent().parent().find(".differenceValueWeight").html(tallyWeight - maxWeight);
				$(this).parent().parent().find(".differenceValueVolume").html(tallyVolume - maxVolume);
			});
			
			//绑定理货重量值改变事件
			$(".tallyWeight").unbind("change").change(function() {
				var weightScale = $(this).attr("weightScale");
				var maxWeight = $(this).parents("tr").find(".tallyQuantity").attr("maxWeight");
				var tallyWeight = $(this).parents("tr").find(".tallyWeight").val();
				$(this).parent().parent().find(".differenceValueWeight").html(tallyWeight - maxWeight);
			});
			
			//绑定理货体积值改变事件
			$(".tallyVolume").unbind("change").change(function() {
				var maxVolume = $(this).parents("tr").find(".tallyQuantity").attr("maxVolume");
				var tallyVolume = $(this).parents("tr").find(".tallyVolume").val();
				$(this).parent().parent().find(".differenceValueVolume").html(tallyVolume - maxVolume);
			});

		}
	
	//显示自理界面
	var showOnselfTally = function(tallyingId) {
		var tpl = $("#tallyDetailTpl2").html();
		$(".tallyDetail_main_content").html(tpl);
	}

	return {
		init: init
	};
});